unit ReceitaDespesaAnualFr;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  ComCtrls, ExtCtrls, DBCtrls, StdCtrls, EditBtn, DBGrids, Buttons, ValEdit,
  Grids, GerencialMainFr;

type

  { TReceitaDespesaFrame }

  TReceitaDespesaFrame = class(TGerencialMainFrame)
    AnoEdit: TEdit;
    Label2: TLabel;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    SpeedButton1: TSpeedButton;
    StringGrid1: TStringGrid;
    procedure SpeedButton1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

implementation

uses
  GerencialDM;

{ TReceitaDespesaFrame }

procedure TReceitaDespesaFrame.SpeedButton1Click(Sender: TObject);
var
  I, J: integer;
  TotalRec, TotalDesp: real;

begin
  if AnoEdit.Text = '' then begin
    ShowMessage('Selecione um Ano!');
  end else begin
    GerencialData.BuscaReceitaDespesaAnual(IntToStr(StrToIntDef(AnoEdit.Text,2013)));
    StringGrid1.Rows[1].Text := 'Receita';
    StringGrid1.Rows[2].Text := 'Despesa';
    StringGrid1.Rows[3].Text := 'Saldo';
    //Insere na tabela os dados Buscados no SQL
    with GerencialData.ReceitaDespesaZQuery do begin
      for I := 1 to RecordCount do begin
        RecNo := I;
        if FieldByName('tipo').AsString = 'R' then begin
          StringGrid1.Cells[FieldByName('mes').AsInteger, 1] := FieldByName('sum').AsString;
          TotalRec := TotalRec + FieldByName('sum').AsFloat;
        end else begin
          StringGrid1.Cells[FieldByName('mes').AsInteger, 2] := FieldByName('sum').AsString;
          TotalDesp := TotalDesp + FieldByName('sum').AsFloat;
        end;
      end;
    end;
    StringGrid1.Cells[13, 1] := FloatToStr(TotalRec);
    StringGrid1.Cells[13, 2] := FloatToStr(TotalDesp);
    for I := 1 to 13 do begin
      StringGrid1.Cells[I, 3] :=
        FloatToStr(StrToFloatDef(StringGrid1.Cells[I, 1],0) - StrToFloatDef(StringGrid1.Cells[I, 2],0));
    end;
  end;
end;

initialization
  {$I receitadespesaanualfr.lrs}

end.
